package com.webside.front.listener;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ServletContextAware;

import com.webside.front.common.mapper.BdictionaryMapper;
import com.webside.front.common.mapper.BsensitivewordMapper;
import com.webside.front.common.model.Bdictionary;
import com.webside.front.listener.vo.Channel;

public class InitialDataListener implements InitializingBean, ServletContextAware {
	@Autowired
	private BdictionaryMapper bdictionaryMapper;
	@Autowired
	private BsensitivewordMapper bsensitivewordMapper;
	private String imgBasePath;

	@Override
	public void setServletContext(ServletContext servletContext) {
		BaseData.img_base_path=imgBasePath;
		
		BaseData.channels = new ArrayList<Channel>();

		List<Bdictionary> dicChannels = bdictionaryMapper.selectAllChannels();
		Channel channel;
		for (int i = 0; i < dicChannels.size(); i++) {
			channel=new Channel();
			Bdictionary dicChannel=dicChannels.get(i);
			channel.setChannel(dicChannel);
			channel.setName(dicChannel.getName());
			channel.setCode(dicChannel.getCode());
			
			channel.setDramaTypes(bdictionaryMapper.selectTypeByChannel(dicChannel));
			channel.setSeriesKinds(bdictionaryMapper.selectKindByChannel(dicChannel));
			BaseData.channels.add(channel);
			System.out.println("init data finished!");
		}
		
		//敏感词加载
		BaseData.Sensitivewords=new HashSet<String>();
		//TODO 从数据库查询出所有敏感词，循环遍历到BaseData.Sensitivewords
		List<String> sensitiveword= bsensitivewordMapper.queryAllSensitiveWord();
		BaseData.Sensitivewords.addAll(sensitiveword);
			
		
		
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub

	}

	public String getImgBasePath() {
		return imgBasePath;
	}

	public void setImgBasePath(String imgBasePath) {
		this.imgBasePath = imgBasePath;
	}
	
	
}
